package com.netsdk.web.controller;

import com.netsdk.demo.module.AlarmListenModule;
import com.netsdk.demo.module.FaceRecognitionModule;
import com.netsdk.demo.module.GateModule;
import com.netsdk.demo.module.LoginModule;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.utils.DeviceUtils;
import com.netsdk.web.callback.AnalyzerCallback;
import com.netsdk.web.callback.MessageCallBack;
import com.sun.jna.Memory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @Author aqie
 * @Date 2022/3/25 11:07
 * @desc
 */
@Slf4j
@RestController
@RequestMapping("test")
public class TestController {

    @GetMapping("handler")
    public String handler(String deviceNo) {
        return LoginModule.handlerMaps.get(deviceNo).toString();
    }

    /**
     * 2709171568224
     * 2709171670432
     */
    @GetMapping("login")
    public String login(String deviceNo) {
        boolean login = DeviceUtils.login(deviceNo);
        log.info("{} ", login);
        return LoginModule.handlerMaps.get(deviceNo).toString();
    }


    @GetMapping("logout")
    public void logout(String deviceNo) {
        DeviceUtils.logout(deviceNo);
    }

    @GetMapping("startListen")
    public Boolean startListen(String deviceNo) {
        AlarmListenModule.startListen(MessageCallBack.getInstance(), deviceNo);
        Boolean aBoolean = AlarmListenModule.listenMap.get(deviceNo);
        return aBoolean;
    }

    @GetMapping("stopListen")
    public Boolean stopListen(String deviceNo) {
        AlarmListenModule.stopListen(deviceNo);
        Boolean aBoolean = AlarmListenModule.listenMap.get(deviceNo);
        return aBoolean;
    }

    /**
     * 1. realLoadPicture
     * 2. stopRealLoadPicture
     * 3. addGroup
     * 4. addPerson
     * @param
     */
    @GetMapping("addGroup")
    public boolean addGroup(String groupName, String deviceNo) {
        boolean b = FaceRecognitionModule.addGroup(groupName, deviceNo);
        return b;
    }

    @GetMapping("addPerson")
    public boolean addPerson(String deviceNo) {
        Memory memory = null;
        String picPath = "D:/download/a.jpg";
        if(!picPath.equals("")) {
            try {
                memory = ToolKits.readPictureFile(picPath);
                boolean aqie = FaceRecognitionModule.addPerson("123",
                        memory, "aqie", 1, true, "2020-01-01", 1, "456", deviceNo);
                return aqie;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    @GetMapping("realLoadPic")
    public String realLoadPic(String deviceNo) {
        NetSDKLib.LLong lLong = GateModule.realLoadPic(0, AnalyzerCallback.getInstance(), deviceNo);
        return lLong.toString();
    }

}
